package com.example.controller.carecommunity.daycare;

import com.example.bean.carecommunity.SheQujujia;
import com.example.bean.carecommunity.Vo.RiZhaoVO;
import com.example.bean.configuration.zhibiaosystem.XzLiebiao;
import com.example.bean.configuration.zhibiaosystem.Zhibiao;
import com.example.bean.institutioncare.institutionadmin.Basicdata;
import com.example.service.carecommunity.daycare.RijianzhaoliaoService;
import com.example.utils.Results;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("/rijianzhaoliao")
@Api(tags = "日间照料中心控制器")
public class RijianzhaoliaoController {
    @Autowired
    RijianzhaoliaoService service;

    @PostMapping("/selectrjzl")
    @ApiOperation(value = "查询所有",notes = "日照服务查询")
    public Results selectRijianzhailiao(@RequestBody RiZhaoVO riZhaoVO){
        PageInfo<SheQujujia> page = service.selectTijianzhaoliaoAll(riZhaoVO);
        return new Results().data("page",page);
    }

    @GetMapping("/getXiangzhi")
    public Results getXiangzhi(){
        String leixiangming="服务功能：";
        Zhibiao zblist = service.getzhibiao(leixiangming);
        List<XzLiebiao> xzlist = service.getxiangzhi(zblist.getNoid());
        List<Basicdata> basicdata = service.getbasiAll();
        return new Results().data("zblist",zblist).data("xzlist",xzlist).data("basicdata",basicdata);
    }

    //上传机构资质文件以及图片信息-到本地
    @PostMapping("/addfile")
    public Map<String,Object> fileupload(MultipartFile file, HttpServletRequest req) {
        Map<String, Object> result = new HashMap<>();

        // 放在本地项目 目录
        //D:\ProjectTraining\养老服务信息系统\picturefile
        String filePath="C:\\images";
        File folder = new File(filePath);

        if (!folder.exists()) {
            folder.mkdirs();
        }
        // 生成新的文件名
        String oldName = file.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
        System.out.println("新生成的文件名："+newName);

        try {
            // 保存文件到指定路径
            file.transferTo(new File(folder, newName));
            String url = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort()+"/back/mystatic/"  + newName;
            System.out.println("路径地址："+url);
            result.put("status", "OK");
            result.put("name", newName);
//            result.put("url", url);

        } catch (IOException e) {
            result.put("status", "ERROR");
            result.put("msg", e.getMessage());
        }
        System.out.println("图片上传："+result);
        return result;
    }

    @PostMapping("/addrizhao")
    public Results addrizhao(@RequestBody SheQujujia rijianzhaoliao){
        return new Results().code(service.addrizhao(rijianzhaoliao));
    }

    @PostMapping("/editRizhaoguanli")
    public Results editRizhaoguanli(@RequestBody List<String> ids){
        Integer i = service.editRizhaoguanli(ids);
        return new Results().code(i);
    }

    @GetMapping("/lookrizhao/{noid}")
    public Results lookrizhao(@PathVariable String noid){
        System.out.println("查看时接收到的id信息："+noid);
        //查询出主表信息
        SheQujujia rzxx = service.lookrizhao(noid);
        System.out.println("查出的日间照料中心信息："+rzxx);
        //查询日间照料服务信息
        Zhibiao zblist = service.getxiangzhilist(noid);
        System.out.println("查出的日间照料中心服务信息："+zblist);
        return new Results().data("rzxx",rzxx).data("zblist",zblist);
    }

    @PutMapping
    public Results updaterizhao(@RequestBody SheQujujia rijianzhaoliao){
        return new Results().code(service.updaterizhao(rijianzhaoliao));
    }
}